package hot_100;

import java.util.PriorityQueue;

public class 数据流的中位数 {
    public static void main(String[] args) {

    }

    PriorityQueue<Integer> prioritySmall = new PriorityQueue<>();
    PriorityQueue<Integer> priorityBig = new PriorityQueue<>((a, b) -> b - a);


    public void addNum(int num) {
        if (prioritySmall.size() == priorityBig.size()) {
            priorityBig.add(num);
            prioritySmall.add(priorityBig.poll());
        } else {
            prioritySmall.add(num);
            priorityBig.add(prioritySmall.poll());
        }
    }

    public double findMedian() {
        if (priorityBig.size() == prioritySmall.size()) {
            return (priorityBig.peek() + prioritySmall.peek()) / 2.0;
        } else {
            return prioritySmall.peek();
        }
    }
}
